package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ToastUtil;

public class DeliveryBaseFragment extends BaseFragment implements OnClickListener {

  protected static final int REQUESST_ORDERNUM = 0;
  protected static final int REQUEST_CONTRACT = 1;
  protected static final int REQUEST_CUSTOMER = 2;
  protected static final int REQUEST_DELIVERTPERSON = 3;
  protected static final int REQUEST_ADDRESS = 5;
  protected static final int REQUEST_LOGISTIC = 6;
  protected static final int REQUEST_IFPAY = 7;
  protected static final int REQUEST_LOGISTICS_COMPANY = 8;
  protected static final String VALUE_TRUE = "T";
  protected static final String VALUE_FALSE = "F";
  public static final int REQUEST_UPDATE = 9;
  //protected static final int REQUEST_EXPRESS = 9;
  protected TextView mOrderNum_addEditDelivery;// 出库订单编号
  protected TextView mRelativeContract_addEditDelivery;// 相关合同
  protected TextView mCustomerNameDelivery_addEditDelivery;// 客户名称
  protected TextView mDeliveryPerson_addEditDelivery;// 配送负责人
  protected EditText mMainGoods_addEditDelivery;// 主要物品

  protected EditText mDeliveryAddress_addEditDelivery;// 配送货地址
  protected ImageView mAddress_addEditDelivery;// 添加配送货地址列表
  protected EditText mDeliveryText_addEditDelivery;// 配送日志
  protected TextView mFfThirdLogistics_addEditDelivery;// 是否第三方物流
  protected EditText mDeliveryFee_addEditDelivery;// 配送费
  protected TextView mFfPay_addEditDelivery;// 是否支付
  protected TextView mLogisticsCompany_addEditDelivery;// 物流公司
  protected EditText mLogisticsNum_addEditDelivery;// 物流单号
  protected TextView mDataState_addEditDelivery;// 数据状态
  protected TextView mRegister_addEditDelivery;// 登记人
  protected TextView mLogisticsDetail_addEditDelivery;// 物流明细
  protected LinearLayout mLl_logistiDetail_addEditDelivery;

  protected String mIfPay;// 是否支付
  protected String mIfThirdLogistics;// 是否第三方物流
  protected String mStaffName;// 配送人名字
  protected String mStaffId;// 配送人id
  protected String mjobNos;// 配送人jobNum
  protected String mCustomerName; // 客户名字
  protected String mCustomerId;// 客户id
  protected String mAddressName;// 地址
  protected String mAddressId;// 地址id
  protected String mOrderNumber;// 订单编号
  protected String mOrderId;// 订单id
  //protected String mMainGoods;// 主要物品
  protected String mLogisticsName;// 物流公司名字
  protected String mLogisticsId;// 物流公司id
  protected String mContractName;// 合同名字
  protected String mContractId;// 合同id
  protected String mContractCode;// 合同编号
  protected BaseVolleyActivity mActivity;

  protected String mDataStatus;
  protected String mId;
  protected CurrentUser mUser;
  protected String mRunId;
  protected String mLogText;// 配送日志
  protected String mDeliveryFee;// 配送费
  protected String mLogisticsNum;// 物流单号
  protected String mRegister;// 登记人
  protected String regStaffId;
  private WebView mwebView;

  @Override public void onCreate(Bundle savedInstanceState) {
    mActivity = (BaseVolleyActivity) getActivity();

    mUser = CurrentUser.newInstance(getActivity());
    mRegister = mUser.getRealName();
    mjobNos = mUser.getJobNumber();
    mwebView = new WebView(getActivity());
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_add_delivery, container, false);
    mOrderNum_addEditDelivery = (TextView) view.findViewById(R.id.tv_orderNum_addEditDelivery);
    mRelativeContract_addEditDelivery =
        (TextView) view.findViewById(R.id.tv_relativeContract_addEditDelivery);
    mCustomerNameDelivery_addEditDelivery =
        (TextView) view.findViewById(R.id.tv_customerNameDelivery_addEditDelivery);
    mDeliveryPerson_addEditDelivery =
        (TextView) view.findViewById(R.id.tv_deliveryPerson_addEditDelivery);
    mMainGoods_addEditDelivery = (EditText) view.findViewById(R.id.tv_mainGoods_addEditDelivery);

    mDeliveryAddress_addEditDelivery =
        (EditText) view.findViewById(R.id.tv_deliveryAddress_addEditDelivery);
    mAddress_addEditDelivery = (ImageView) view.findViewById(R.id.iv_address_addEditDelivery);
    mDeliveryText_addEditDelivery =
        (EditText) view.findViewById(R.id.tv_deliveryText_addEditDelivery);
    mFfThirdLogistics_addEditDelivery =
        (TextView) view.findViewById(R.id.tv_ifThirdLogistics_addEditDelivery);
    mDeliveryFee_addEditDelivery =
        (EditText) view.findViewById(R.id.tv_deliveryFee_addEditDelivery);
    mFfPay_addEditDelivery = (TextView) view.findViewById(R.id.tv_ifPay_addEditDelivery);

    mLogisticsCompany_addEditDelivery =
        (TextView) view.findViewById(R.id.tv_logisticsCompany_addEditDelivery);
    mLogisticsNum_addEditDelivery =
        (EditText) view.findViewById(R.id.tv_logisticsNum_addEditDelivery);
    mDataState_addEditDelivery = (TextView) view.findViewById(R.id.tv_dataState_addEditDelivery);
    mRegister_addEditDelivery = (TextView) view.findViewById(R.id.tv_register_addEditDelivery);
    mLogisticsDetail_addEditDelivery =
        (TextView) view.findViewById(R.id.tv_logisticsDetail_addEditDelivery);
    mLl_logistiDetail_addEditDelivery =
        (LinearLayout) view.findViewById(R.id.ll_logistiDetail_addEditDelivery);

    mDataState_addEditDelivery.setText(R.string.draft);
    mRegister_addEditDelivery.setText(mRegister);
    mDeliveryPerson_addEditDelivery.setText(mRegister);
    mFfThirdLogistics_addEditDelivery.setText(R.string.isFalse);
    mLogisticsCompany_addEditDelivery.setEnabled(false);
    mLogisticsNum_addEditDelivery.setEnabled(false);
    mFfPay_addEditDelivery.setText(R.string.isFalse);
    mLl_logistiDetail_addEditDelivery.setVisibility(View.GONE);
    mOrderNum_addEditDelivery.setOnClickListener(this);
    mRelativeContract_addEditDelivery.setOnClickListener(this);
    mCustomerNameDelivery_addEditDelivery.setOnClickListener(this);
    mDeliveryPerson_addEditDelivery.setOnClickListener(this);
    mMainGoods_addEditDelivery.setOnClickListener(this);
    mAddress_addEditDelivery.setOnClickListener(this);
    mFfThirdLogistics_addEditDelivery.setOnClickListener(this);
    mFfPay_addEditDelivery.setOnClickListener(this);
    mLogisticsCompany_addEditDelivery.setOnClickListener(this);
    mLogisticsNum_addEditDelivery.setOnClickListener(this);
    mDataState_addEditDelivery.setOnClickListener(this);
    initView();
    return view;
  }

  public void initView() {

  }

  @Override public void onClick(View v) {
    Intent intent;
    switch (v.getId()) {
      //// 出库订单编号
      case R.id.tv_orderNum_addEditDelivery:
        intent = new Intent(getActivity(), OrderNumberListActivity.class);
        startActivityForResult(intent, REQUESST_ORDERNUM);
        break;
      // 相关合同
      case R.id.tv_relativeContract_addEditDelivery:
        intent = new Intent(getActivity(), ContractListActivity.class);
        if (mOrderId != null) {
          intent.putExtra(ContractListFragment.EXTRA_ORDER_ID, mOrderId);
        }
        startActivityForResult(intent, REQUEST_CONTRACT);
        break;
      // 客户名称
      case R.id.tv_customerNameDelivery_addEditDelivery:
        showDialog(REQUEST_CUSTOMER, null);
        break;
      // 配送负责人
      case R.id.tv_deliveryPerson_addEditDelivery:
        showDialog(REQUEST_DELIVERTPERSON, null);
        break;
      // 配送货地址
      case R.id.iv_address_addEditDelivery:
        if (TextUtils.isEmpty(mCustomerNameDelivery_addEditDelivery.getText().toString())) {
          ToastUtil.show(R.string.chooseCustomer);
        } else {
          showDialog(REQUEST_ADDRESS, null);
        }
        break;
      // 是否第三方物流
      case R.id.tv_ifThirdLogistics_addEditDelivery:
        showDialog(REQUEST_LOGISTIC, null);
        break;
      // 是否支付
      case R.id.tv_ifPay_addEditDelivery:
        showDialog(REQUEST_IFPAY, null);
        break;
      // 物流公司
      case R.id.tv_logisticsCompany_addEditDelivery:
        showDialog(REQUEST_LOGISTICS_COMPANY, null);
        break;

      case R.id.tv_logisticsDetail_addEditDelivery:
        // intent = new Intent(getActivity(),
        // DeliveryExpressLookActivity.class);
        // startActivityForResult(intent, REQUEST_EXPRESS);
        initLosistic();
        break;
      default:
        break;
    }
  }

  private void initLosistic() {
    // 加载需要显示的网页
    mwebView.loadUrl("http://www.kuaidi100.com/");
    WebSettings webSettings = mwebView.getSettings();
    webSettings.setSupportZoom(true); // 支持缩放
    // 设置WebView属性，能够执行Javascript脚本
    webSettings.setJavaScriptEnabled(true);
    // html页面大小自适应
    //noinspection deprecation
    webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
  }

  protected void showDialog(int requestCode, @SuppressWarnings("UnusedParameters") Object object) {
    DialogFragment dialog = null;
    Bundle bundle;

    switch (requestCode) {
      // 客户名称
      case REQUEST_CUSTOMER:
        dialog = new CustomerDeliveryDialogFragment();
        break;
      // 配送负责人
      case REQUEST_DELIVERTPERSON:
        Intent i = new Intent(getActivity(), AppointerListActivity.class);
        startActivityForResult(i, requestCode);
        return;

      // 配送货地址
      case REQUEST_ADDRESS:
        dialog = new DeliveryAddressDialogFragment();
        bundle = new Bundle();
        bundle.putString(DeliveryAddressDialogFragment.EXTRA_CUSTOMERID, mCustomerId);
        dialog.setArguments(bundle);
        break;
      // 是否第三方物流
      case REQUEST_LOGISTIC:
        dialog = new DeliveryIfPayDialogFragment();
        bundle = new Bundle();
        bundle.putString(DeliveryIfPayDialogFragment.REQUEST_TYPE,
            DeliveryIfPayDialogFragment.EXTRA_LOGISTICS);
        dialog.setArguments(bundle);
        break;
      // 是否支付
      case REQUEST_IFPAY:
        dialog = new DeliveryIfPayDialogFragment();
        bundle = new Bundle();
        bundle.putString(DeliveryIfPayDialogFragment.REQUEST_TYPE,
            DeliveryIfPayDialogFragment.EXTRA_IFPAY);
        dialog.setArguments(bundle);
        break;
      // 物流公司
      case REQUEST_LOGISTICS_COMPANY:
        dialog = new LogisticsCompanyDialogFragment();
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (dialog == null) return;
    dialog.setTargetFragment(DeliveryBaseFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    // 出库订单编号
    if (requestCode == REQUESST_ORDERNUM && data != null) {
      mOrderNumber = data.getStringExtra(OrderNumberListFragment.REQUEST_ORDER_NUMBER);
      mOrderId = data.getStringExtra(OrderNumberListFragment.REQUEST_ORDER_ID);
      mContractName = data.getStringExtra(OrderNumberListFragment.REQUEST_CONTRACT_NAME);
      mContractId = data.getStringExtra(OrderNumberListFragment.REQUEST_CONTRACT_ID);
      mCustomerName = data.getStringExtra(OrderNumberListFragment.REQUEST_CUSTOMER_NAME);
      mCustomerId = data.getStringExtra(OrderNumberListFragment.REQUEST_CUSTOMER_ID);
      mAddressName = data.getStringExtra(OrderNumberListFragment.REQUEST_ADDRESS_NAME);
      //mMainGoods = data.getStringExtra(OrderNumberListFragment.REQUEST_MAIN_GOODS);
      //LogUtil.i("mOrderNumber==="
      //    + mOrderNumber
      //    + "mOrderId===="
      //    + mOrderId
      //    + "mContractName===="
      //    + mContractName
      //    + "mContractId===="
      //    + "mCustomerName===="
      //    + mCustomerName
      //    + "mCustomerId===="
      //    + mCustomerId
      //    + "mAddressName===="
      //    + mAddressName
      //    + "mMainGoods==="
      //    + mMainGoods);
      if (mOrderNumber != null) {
        mOrderNum_addEditDelivery.setText(mOrderNumber);
      }

      if (mCustomerName != null) {
        mCustomerNameDelivery_addEditDelivery.setText(mCustomerName);
      }
      if (mAddressName != null) {
        mDeliveryAddress_addEditDelivery.setText(mAddressName);
      }

      if (mContractName != null) {
        mRelativeContract_addEditDelivery.setText(mContractName);
      } else {
        mRelativeContract_addEditDelivery.setText("");
      }

      //if (mMainGoods != null) {
      //  mMainGoods_addEditDelivery.setText(mMainGoods);
      //}
    }
    // 相关合同
    if (requestCode == REQUEST_CONTRACT && data != null) {
      mContractId = data.getStringExtra(ContractListFragment.REQUEST_CONTRACT_ID);
      mContractCode = data.getStringExtra(ContractListFragment.REQUEST_CONTRACT_CODE);
      mContractName = data.getStringExtra(ContractListFragment.REQUEST_CONTRACT_NAME);
      LogUtil.i("mContractId==="
          + mContractId
          + "mContractCode===="
          + mContractCode
          + "mContractName==="
          + mContractName);
      if (mContractCode != null) {
        mRelativeContract_addEditDelivery.setText(mContractCode);
      }
    }
    // 客户名称
    if (requestCode == REQUEST_CUSTOMER && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mCustomerName = customer.getName();
      mCustomerId = customer.getId();
      LogUtil.i("mCustomerName===" + mCustomerName + "mCustomerId====" + mCustomerId);
      if (mCustomerName != null) {
        mCustomerNameDelivery_addEditDelivery.setText(mCustomerName);
      }
    }
    // 配送负责人
    if (requestCode == REQUEST_DELIVERTPERSON && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mStaffName = customer.getName();
      //			mStaffId = customer.getId();
      mjobNos = customer.getId();//id其实是jobNo
      LogUtil.i("mStaffName===" + mStaffName + "mStaffId====" + mStaffId + "mjobNos===" + mjobNos);
      String name = getString(R.string.nothingAny);
      if (name.equalsIgnoreCase(mStaffName)) {
        mDeliveryPerson_addEditDelivery.setText("");
      } else {
        mDeliveryPerson_addEditDelivery.setText(mStaffName);
      }
    }
    // 配送货地址
    if (requestCode == REQUEST_ADDRESS && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mAddressName = customer.getName();
      mAddressId = customer.getId();
      LogUtil.i("mAddressName===" + mAddressName + "mAddressId====" + mAddressId);
      if (mAddressName != null) {
        mDeliveryAddress_addEditDelivery.setText(mAddressName);
      }
    }

    // 是否第三方物流
    if (requestCode == REQUEST_LOGISTIC && data != null) {
      mIfThirdLogistics = data.getStringExtra(DeliveryIfPayDialogFragment.EXTRA_THIRD_LOGISTICS);
      if (VALUE_TRUE.equalsIgnoreCase(mIfThirdLogistics)) {
        mFfThirdLogistics_addEditDelivery.setText(R.string.isTrue);
        mLogisticsCompany_addEditDelivery.setEnabled(true);
        mLogisticsNum_addEditDelivery.setEnabled(true);
      } else if (VALUE_FALSE.equalsIgnoreCase(mIfThirdLogistics)) {
        mFfThirdLogistics_addEditDelivery.setText(R.string.isFalse);
        mLogisticsCompany_addEditDelivery.setEnabled(false);
        mLogisticsNum_addEditDelivery.setEnabled(false);
        mLogisticsCompany_addEditDelivery.setText("");
        mLogisticsNum_addEditDelivery.setText("");
      }
    }
    // 是否支付
    if (requestCode == REQUEST_IFPAY && data != null) {
      mIfPay = data.getStringExtra(DeliveryIfPayDialogFragment.EXTRA_PAY);
      if (VALUE_TRUE.equalsIgnoreCase(mIfPay)) {
        mFfPay_addEditDelivery.setText(R.string.isTrue);
      } else if (VALUE_FALSE.equalsIgnoreCase(mIfPay)) {
        mFfPay_addEditDelivery.setText(R.string.isFalse);
      }
    }
    // 物流公司
    if (requestCode == REQUEST_LOGISTICS_COMPANY && data != null) {
      CustomerDialog customer =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mLogisticsName = customer.getName();
      mLogisticsId = customer.getId();
      LogUtil.i("mLogisticsName===" + mLogisticsName + "mLogisticsId====" + mLogisticsId);
      mLogisticsCompany_addEditDelivery.setText(mLogisticsName);
    }
    /** 由于没有详情接口,目前更新只能在列表更新*/
    if (requestCode == REQUEST_UPDATE) {
      mActivity.setResult(Activity.RESULT_OK);
      mActivity.finish();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
